# -*- coding: utf-8 -*-
# @Time    : 2021/11/13 19:10
# @Author  : travel2040
# @Email   : travel2040@163.com
# @File    : tcp-上传屏幕截图.py
# 需要安装的库
# pip install pillow
# pip install numpy
# pip install opencv_python

import socket
import cv2
import numpy as np
from PIL import Image
from PIL import ImageGrab


def tcpUpload():

    host = "127.0.0.1"
    port = 8080

    client= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect((host,port))
    connection = client.makefile('wb')

    capture_image = ImageGrab.grab()  # 抓取屏幕
    frame = cv2.cvtColor(np.array(capture_image), cv2.COLOR_RGB2BGR)

    fs = frame.shape

    size = (int(fs[1] / 2), int(fs[0] / 2))
    frame = cv2.resize(frame, size, interpolation=cv2.INTER_AREA)

    _, buf = cv2.imencode(".jpg", frame)
    b = Image.fromarray(np.uint8(buf)).tobytes()

    connection.write(b)
    connection.flush()

    connection.close()
    client.close()

if __name__ == '__main__':
    tcpUpload()
